package projet.camping.sevlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import projet.camping.exception.ArgumentInvalideException;
import projet.camping.exception.PartieNonExistantException;
import projet.camping.usecases.GestionPartie;

@WebServlet(urlPatterns = "/attenteJoueur.html")
public class AttenteJoueur extends javax.servlet.http.HttpServlet implements
javax.servlet.Servlet {
	@EJB 
	GestionPartie ucc;
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.sendRedirect(resp.encodeRedirectURL("campingBattle.jsp"));
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		HttpSession session = req.getSession();
		PrintWriter out = resp.getWriter();
		
		try {
			if(ucc.lancerPartie((String)session.getAttribute("nomPartie"))){
				out.write("ok");
			}
		} catch (PartieNonExistantException e) {
			out.write("partieNonExistante");
			System.err.println("la partie n'existe pas");
		} catch (ArgumentInvalideException e) {
			e.printStackTrace();
		}
	}
}
